<?php

namespace Admin\Models\school;

use Admin\Models\BaseModel;
class XhschoolClasslist extends BaseModel
{

    /**
     *
     * @var integer
     */
    public $iSCLId;

    /**
     *
     * @var integer
     */

    public $iSOCId;
    /**
     *
     * @var string
     */
    public $sClassId;

    /**
     *
     * @var string
     */
    public $sClassName;

    /**
     *
     * @var integer
     */
    public $iOp;

    /**
     *
     * @var integer
     */
    public $bDelete;

    /**
     *
     * @var string
     */
    public $tCreateDate;

    /**
     *
     * @var string
     */
    public $iCreateUserId;

    /**
     *
     * @var string
     */
    public $tLastModifyDate;

    /**
     *
     * @var string
     */
    public $iLastModifyUserId;

    /**
     * Initialize method for model.
     */
    public function initialize()
    {
        parent::initialize();
        $this->setSource("XHSchool_ClassList");
    }

    /**
     * Returns table name mapped in the model.
     *
     * @return string
     */
    public function getSource()
    {
        return 'XHSchool_ClassList';
    }

    /**
     * Allows to query a set of records that match the specified conditions
     *
     * @param mixed $parameters
     * @return XhschoolClasslist[]
     */
    public static function find($parameters = null)
    {
        return parent::find($parameters);
    }

    /**
     * Allows to query the first record that match the specified conditions
     *
     * @param mixed $parameters
     * @return XhschoolClasslist
     */
    public static function findFirst($parameters = null)
    {
        return parent::findFirst($parameters);
    }

    /**
     * 根据开班ID删除相关班级记录
     *
     * @param $openClassId
     * @return mixed
     *
     * create by ck 20160929
     */
    public static function deleteRecordByOpenClassId($openClassId)
    {
        return self::find([
            'conditions'    => 'iSOCId = :openClassId:',
            'bind'          => ['openClassId' => $openClassId]
        ])->delete();
    }

    /**
     * 创建记录
     *
     * @param $openClassId              ---开班ID
     * @param $className                --班级名称
     * @param $classId                  --班级ID
     * @param int $op                   --操作码
     * @return XhschoolClasslist
     *
     * create by ck 20160816
     */
    public static function createRecord($openClassId, $className, $classId = null, $op = 0){
        $obj = new self;
        $obj->sClassId      = $classId;
        $obj->iSOCId        = $openClassId;
        $obj->sClassName    = $className;
        $obj->iOp           = $op;
        $obj->bDelete       = 0;
        return $obj;
    }

    /**
     * 根据开班ID获取信息
     *
     * @param $openClassId
     * @return XhschoolClasslist[]
     *
     * create by ck 20160816
     */
    public static function findByOpenClassId($openClassId){
        return self::find(array(
            'conditions'    => 'iSOCId = :iOpenClassId: AND bDelete = 0',
            'bind'          => array(
                'iOpenClassId'  => $openClassId
            )
        ));
    }

    /**
     * 根据开班ID获取班级ID数组
     *
     * @param $openClassId
     * @return array
     *
     * create by ck 20160929
     */
    public static function getClassIdByOpenClassId($openClassId)
    {
        $class = self::find(array(
            'conditions'    => 'iSOCId = :iOpenClassId:',
            'bind'          => array(
                'iOpenClassId'  => $openClassId
            )
        ))->toArray();
        if(!$class) return false;
        return array_column($class, 'iSCLId');
    }

}
